package com.lyp.crm.conrtoller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lyp.crm.entity.Fenye;
import com.lyp.crm.entity.Modules;
import com.lyp.crm.entity.Node;
import com.lyp.crm.entity.Rolesmodules;
import com.lyp.crm.service.Modulesdao;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@CrossOrigin(origins = "*")
public class ModulesController {
	@Autowired
	private Modulesdao modulesdao;
	@Autowired
	private Fenye<Modules> fenye;
/**
 * 
 * @param page
 * @param rows
 * @return
 */
	@ApiOperation(value="查询所有模块", notes="查询所有模块")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "分页参数", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "rows", value = "分页参数", required = true, dataType = "Integer", paramType = "path"), 
		
	})
	@RequestMapping(value="/ShowModules",method=RequestMethod.POST)
	@ResponseBody
	public Fenye<Modules> SelectModules(Integer page, Integer rows) {
		fenye.setPage((page-1)*rows);
		fenye.setPageSize(rows);
		fenye=modulesdao.SelectModules(fenye);
		return fenye;
	}
	/**
	 * 
	 * @param modules
	 * @return
	 */
	
	@ApiOperation(value="多条件查询模块", notes="根据 url 的各种数据来获取模块的详细信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mname", value = "模块名称", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "mfather", value = "父模块编号", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "murl", value = "模块路径", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "mweight", value = "权重", required = true, dataType = "String", paramType = "path"), 
		
	})
	@RequestMapping(value="/SelectModuleslist",method=RequestMethod.POST)
	@ResponseBody
	public List<Modules> SelectModuleslist(Modules modules) {
		
		return modulesdao.SelectModuleslist(modules); 
	}
	/**
	 * 
	 * @param rid
	 * @return
	 */
	
	@ApiOperation(value="查询我的模块", notes="根据 url 的id来获取模块的详细信息")
	@ApiImplicitParam(name = "rid", value = "用户id", required = true, dataType = "Integer", paramType = "path") 
	
	@RequestMapping(value="/MyModules",method=RequestMethod.POST)
	@ResponseBody
	public List<Modules> MyModules(Integer rid) {
		List<Modules> list = modulesdao.mymodules(rid);
		return list;
	}
	
	@ApiOperation(value="增加模块", notes="根据 url 的各种数据来增加模块")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mname", value = "模块名称", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "mfather", value = "父模块编号", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "murl", value = "模块路径", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "mweight", value = "权重", required = true, dataType = "String", paramType = "path"), 
		
	})
	@RequestMapping(value="/insertModules",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertModules(Modules modules) {
		//��ѯͬһ����ģ�����Ƿ��Ѿ�����ͬ��ģ��
		Integer selectinsbymnanme = modulesdao.selectinsbymnanme(modules);
		//��ͬ��ģ��
		if (selectinsbymnanme>0) {
			return 0;
		} else {
			return modulesdao.insertModules(modules);
		}
	}

	
	@ApiOperation(value="查询模块", notes="根据 url 的模块id来获取模块的详细信息")
	@ApiImplicitParam(name = "mid", value = "模块id", required = true, dataType = "Integer", paramType = "path")
	
	@RequestMapping(value="/deleteModules",method=RequestMethod.POST)
	@ResponseBody
	public Integer deleteModules(Integer mid) {
		//����һ���м�����
		Rolesmodules rolesmodules=new Rolesmodules();
		//�ѻ�õ�ģ��id�����м���е�ģ��id
		rolesmodules.setRmmid(mid);
		//�����м������ѯ��ģ���Ƿ�ռ��
		Integer selectrolesmodules = modulesdao.selectrolesmodules(rolesmodules);
		
		if (selectrolesmodules>0) {
			//ռ�÷���0
			return 0;
		} else {
			return modulesdao.deleteModules(mid);
		}
		
	}
	@ApiOperation(value="更新模块", notes="根据 url 的各种数据来更新模块")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mid", value = "模块编号", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "mname", value = "模块名称", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "mfather", value = "父模块编号", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "murl", value = "模块路径", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "mweight", value = "权重", required = true, dataType = "String", paramType = "path"), 
		
	})
	@RequestMapping(value="/updateModules",method=RequestMethod.POST)
	@ResponseBody
	public Integer updateModules(Modules modules) {
		//����ģ��id��ѯ��ģ�飨��ȡ����id��
		Modules modulesmid = modulesdao.selectModulesmid(modules.getMid());
		//���ݴ�ģ���ѯ��ģ�����Ƿ���ͬ��
		Integer selectinsbymnanme = modulesdao.selectinsbymnanme(modulesmid);
		
		if (selectinsbymnanme>0) {
			return 0;
		} else {
			return modulesdao.updateModules(modules);
		}
	}
	
	
	
	@ApiOperation(value="增加角色模块", notes="根据 url 传来的数据来增加角色模块")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rmrid", value = "角色id", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "rmmid", value = "模块id", required = true, dataType = "Integer", paramType = "path"), 
		
	})
	@RequestMapping(value="/insertrolesmodules",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertrolesmodules(Rolesmodules rolesmodules) {
		
		return modulesdao.insertrolesmodules(rolesmodules);
	}
	
	@ApiOperation(value="查询角色模块信息", notes="根据 url 传来的数据来查询角色模块")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rmrid", value = "角色id", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "rmmid", value = "模块id", required = true, dataType = "Integer", paramType = "path"), 
		
	})
	@RequestMapping(value="/selectrolesmodules",method=RequestMethod.POST)
	@ResponseBody
	public Integer selectrolesmodules(Rolesmodules rolesmodules) {
		return modulesdao.selectrolesmodules(rolesmodules);
	}
	@ApiOperation(value="删除角色模块信息", notes="根据 url 传来的角色id来删除角色模块")
	@ApiImplicitParam(name = "rmrid", value = "角色id", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value="/deletelesmodules",method=RequestMethod.POST)
	@ResponseBody
	public Integer deletelesmodules(Integer rmid) {
		Integer modules = modulesdao.selectbymidModules(rmid);
		if (modules>0) {
			return 0;
		}else {
			return modulesdao.deletelesmodules(rmid);
		} 
		
	}
	@ApiOperation(value="查询模块总条数", notes="根据 url 传来的模块id和模块名称查询模块总条数")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mid", value = "模块编号", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "mname", value = "模块名称", required = true, dataType = "String", paramType = "path"), 
	
	})
	@RequestMapping(value="/selectbymidModules",method=RequestMethod.POST)
	@ResponseBody
	public Integer selectbymidModules(Integer mid) {
		// TODO Auto-generated method stub
		return modulesdao.selectbymidModules(mid);
	}
	@ApiIgnore
	@RequestMapping(value = "/selectRolesModulesTree", method = RequestMethod.POST)
		@ResponseBody
		public List<Node> selectRolesModulesTree(Integer rid) {
			return modulesdao.selectRolesModulesTree(rid);
		}
	
}
